module ModelSchemaInfo
  class Console
    def show(result)
      puts ""
      puts "#{result.model_name} ======================================================================================="
      puts ""

      puts 'COLUMNS-----------------------------------------------------------------------'
      puts ''
      puts '|-------------------------------------------------------------------|'
      puts '|  sql type       |   attribute_name                                |'
      puts '|-------------------------------------------------------------------|'
      puts_columns(result)
      puts '|-------------------------------------------------------------------|'
      puts ''
      puts 'ASSOSIATIONS------------------------------------------------------------------'
      puts ''
      puts_assosiations(result)
      puts ''
      puts '------------------------------------------------------------------------------'
      puts ''
      puts ''
    end

    private

      def puts_columns(result)
        result.columns.each do |column|
          sql_type = "#{column.sql_type}#{' ' * 15}".slice(0, 15)
          column_name = "#{column.name}#{' ' * 47}".slice(0, 47)
          puts "|  #{sql_type}|  #{column_name}|"
        end
      end

      def puts_assosiations(result)
        result.assosiations.each do |assosiation|
          puts "* #{assosiation.name}"
          if foreign_key = assosiation.options[:foreign_key]
            puts "-- foreign_key: #{foreign_key}"
          end
          puts ""
        end
      end

  end
end

